import type {
  ExtractFormStringifyFromObject,
  ExtractFormStringifyFromList,
  FormItemConfigList,
  FormSearchInstance,
  FormSearchProps,
  FormSearchSearchType,
} from "@/components/form";
import type {
  TableApiParams,
  TableApiResult,
  TableMainInstance,
  TableMainProps,
} from "@/components/table";

export type FormSearchOptionsKey = "staticQuery";

export type FormSearchRewriteKey = "list";

export type TableMainOptionsKey = "refine" | "refineReduceHeight";

export type TableMainRequiredKey = "columns" | "rowKey";

export type TableMainRewriteKey = "api" | "query" | "injectInfo";

export type ListPageInnerInjectInfo<
  PO extends Record<string, any>,
  /** 期望注入到表格列插槽上的额外内容 */
  F extends Record<string, any>,
> = Pick<ListPageInstance<PO>, "triggerReset" | "triggerSearch" | "update"> & F;

export type ListPageProps<
  /** 行数据结构 */
  T extends Record<string, any>,
  /** 表单标准结构模型 */
  PO extends Record<string, any>,
  /** 表单需转换的部分结构模型 */
  SO extends Record<string, any>,
  /** 静态参数 */
  SQ extends Record<string, any>,
  /** 期望注入到表格列插槽上的额外内容 */
  F extends Record<string, any>,
> = Partial<Pick<FormSearchProps<PO, SO, {}>, FormSearchOptionsKey>> & {
  formSearchProps?: Partial<
    Omit<
      FormSearchProps<PO, SO, {}>,
      FormSearchOptionsKey | FormSearchRewriteKey
    >
  >;
} & Pick<
    TableMainProps<
      T,
      ExtractFormStringifyFromObject<PO, SO> & SQ,
      ListPageInnerInjectInfo<PO, F>
    >,
    TableMainRequiredKey
  > &
  Partial<
    Pick<
      TableMainProps<
        T,
        ExtractFormStringifyFromObject<PO, SO> & SQ,
        ListPageInnerInjectInfo<PO, F>
      >,
      TableMainOptionsKey
    >
  > & {
    tableMainProps?: Partial<
      Omit<
        TableMainProps<
          T,
          ExtractFormStringifyFromObject<PO, SO> & SQ,
          ListPageInnerInjectInfo<PO, F>
        >,
        TableMainOptionsKey | TableMainOptionsKey | TableMainRewriteKey
      >
    >;
  } & {
    /** 是否展示操作区 */
    showOperation?: boolean;
    list?: FormItemConfigList<PO, SO>;
    /** api */
    api: (
      params: TableApiParams<ExtractFormStringifyFromObject<PO, SO> & SQ>,
    ) => Promise<TableApiResult<T>>;
    /** 静态参数 */
    query?: SQ;
    injectInfo?: F;
    /** 搜索前的回调[同步] */
    beforeSearch?: (
      data: ExtractFormStringifyFromObject<PO, SO>,
      type: FormSearchSearchType,
    ) => ExtractFormStringifyFromObject<PO, SO>;
    /** 在点击搜索重置按钮时，是否同时调用清空表格表头过滤 */
    clearTableFilterOnReset?: boolean;
  };

export interface ListPageInstance<
  PO extends Record<string, any> = Record<string, any>,
> extends FormSearchInstance,
    TableMainInstance {
  /** 更新表单数据 */
  update: (key: Extract<keyof PO, string>, value: any) => void;
}

export type ListApiParams<L, SQ extends Record<string, any>> = TableApiParams<
  ExtractFormStringifyFromList<L> & SQ
>;
